package yxy.game.pm2.module.chibi;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import yxy.apple.util.concurrent.ThreadPoolManager;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.chibirankreward;
import yxy.cherry.data.string.GameString;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.bean.module.rank.PlayerRankModule;
import yxy.game.pm2.bean.module.rank.RankEntry;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.pubsub.ArenaRankPublisher;
import yxy.game.pm2.module.mail.MailHandle;
import yxy.game.pm2.module.mail.MailTypeEnum;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

public class RankChibi extends PlayerRankModule {

	public RankChibi(AreaBean areaBean) {
		super(areaBean);
	}

	@Override
	public String rankName() {
		return PlayerRankModule.getAllRankNames().get(1);
	}

	@Override
	protected boolean reconsitution() {
		return false;
	}

	@Override
	protected long valuePlastic(long value) {
		return value * 10000000000000L + (10000000000000L - DateTime.now().getMillis());// 13位时间戳长度
	}

	@Override
	protected long valueRestore(long score) {
		return score / 10000000000000L;
	}

	@Override
	public void handleUpdate(DateTime ori, DateTime now) {
		if (new LocalDate(now).equals(new LocalDate(ori))) {
			return;
		}

		if (new LocalDate(now).getWeekOfWeekyear() != new LocalDate(ori).getWeekOfWeekyear()) {
			ArrayList<RankEntry> weekyTop = weekyTop();
			clear();// 清排行
			// 星期不同 发赛季结算
			rewardWeekRank(weekyTop);

//			ThreadPoolManager.getInstance().execute(new Runnable() {
//				@Override
//				public void run() {
//					ArrayList<RankEntry> weekyTop = weekyTop();
//					clear();// 清排行
//					// 星期不同 发赛季结算
//					rewardWeekRank(weekyTop);
//				}
//			});
		}
	}

	private ArrayList<RankEntry> weekyTop() {
		int min = -1;// 全局最小
		int max = -1;// 全局最大
		Map<String, chibirankreward> all = DataCenter.getAllData(chibirankreward.class);
		for (chibirankreward data : all.values()) {
			if (min < 0 || data.getRankingmin() < min) {
				min = data.getRankingmin();
			}
			if (max < 0 || data.getRankingmax() > max) {
				max = data.getRankingmax();
			}
		}
		return new ArrayList<>(range(min, max));
	}

	private void rewardWeekRank(ArrayList<RankEntry> weekyTop) {
		System.err.println("火烧赤壁发排名奖励了！！！！" + DateTime.now().toString());
		Map<String, chibirankreward> all = DataCenter.getAllData(chibirankreward.class);
		for (chibirankreward data : all.values()) {
			Collection<Currency> rewards = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
			for (int i = data.getRankingmin() - 1; i < data.getRankingmax() && i < weekyTop.size(); i++) {
				int rank = i + 1;
				RankEntry entry = weekyTop.get(i);
				String title = GameString.火烧赤壁排行奖励_titl(rank).getText();
				String content = GameString.火烧赤壁排行奖励_msg(rank).getText();
				new MailHandle().sendMail(entry.getKey(), MailTypeEnum.Mail, title, content, CCReason.竞技场, rewards);

				ArenaRankPublisher.getInstance().publish(entry.getKey(), rank);
			}
		}
	}

	static public RankChibi getRank(String playerCode) {
		AreaBean areaBean = AreaModule.area(playerCode);
		return new RankChibi(areaBean);
	}

}
